#include "HttpServer.hpp"
#include <memory>

using namespace std;
using namespace server;

void Usage(std::string proc)
{
    cerr << "Usage:\n\t" << proc << " port\r\n\r\n";
}

std::string suffixToDesc(const std::string suffix)
{
    std::string ct = "Content-Type: ";
    if (suffix == ".html")
        ct += "text/html";
    else if (suffix == ".jpg")
        ct += "application/x-jpg";
    else if (suffix == ".gif")
        ct += "image/gif";
    ct += "\r\n";
    return ct;
}
// 1. 服务器和网页分离，html
// 2. url -> / : web根目录
// 3. 我们要正确的给客户端返回资源类型，我们首先要自己知道！所有的资源都有后缀！！
bool Get(const HttpRequest &req, HttpResponse &resp)
{
    // if(req.path == "test.py")
    // {
    //     //建立进程间通信，pipe
    //     //fork创建子进程，execl("/bin/python", test.py)
    //     // 父进程，将req.parm 通过管道写给某些后端语言，py，java，php等语言
    // }
    // if(req.path == "/search")
    // {
    //     // req.parm
    //     // 使用我们自己写的C++的方法，提供服务
    // }
    // for test
    // 输出请求的报头信息
    cout << "----------------------http start---------------------------" << endl;
    cout << req.inbuffer << std::endl;
    std::cout << "method: " << req.method << std::endl;
    std::cout << "url: " << req.url << std::endl;
    std::cout << "httpversion: " << req.httpversion << std::endl;
    std::cout << "path: " << req.path << std::endl;
    std::cout << "suffix: " << req.suffix << std::endl;
    std::cout << "size: " << req.size << "字节" << std::endl;
    cout << "----------------------http end---------------------------" << endl;

    std::string respline = "HTTP/1.1 200 OK\r\n";
    std::string respheader = suffixToDesc(req.suffix);
    // 设置Cookie，以后的请求都会有Cookie
    respheader += "Set-Cookie: name=1234567abcdefg; Max-Age=120\r\n";
    // 往后，每次http请求，都会自动携带曾经设置的所有的cookie，帮服务器进行鉴权行为 --- http会话保持

    // if (req.size > 0)
    // {
    //     respheader += "Content-Length: ";
    //     respheader += std::to_string(req.size);
    //     respheader += "\r\n";
    // }

    // respheader += "Location: https://www.qq.com/\r\n";
    std::string respblank = "\r\n";
    // std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>http会话保持（客户端Cookie，服务器session）</h1></head><body><p>例如我们打开哔哩哔哩的首页进行登录操作，哪怕用户马上把浏览器关了，短期内再次访问哔哩哔哩是不需要用户重复登录操作的。<br>同样的，当我们在哔哩哔哩进行页面跳转时，http协议并不会记录用户信息，但是曾经的用户登录信息在页面跳转时并不会丢失。浏览器仍会记住上一次登录的信息。这就是会话保持。</p></body></html>";

    // std::string body = "<h1>hello world</h2>";
    std::string body;
    body.resize(req.size + 1);
    if (!Util::readFile(req.path, &body, req.size)) // 没找到资源就把404.html构建响应返回
    {
        Util::readFile(html_404, &body, req.size); // 一定能成功，404.html自己写了一定有
    }

    respheader += "Content-Length: ";
    respheader += std::to_string(body.size());
    respheader += "\r\n";
    resp.outbuffer += respline;
    resp.outbuffer += respheader;
    resp.outbuffer += respblank;

    cout << "----------------------http response start---------------------------" << endl;
    std::cout << resp.outbuffer << std::endl;
    cout << "----------------------http response end---------------------------" << endl;

    resp.outbuffer += body;

    return true;
}

// ./httpServer 8080
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<HttpServer> httpsvr(new HttpServer(Get, port));

    // httpsvr->registerCb("/", Get); // 功能路由！
    // httpsvr->registerCb("/search", Search);
    // httpsrv->registerCb("/test.py", Other);
    httpsvr->initServer();
    httpsvr->start();

    return 0;
}